/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package l2s.gameserver.scripts.quests;

import l2s.commons.util.Rnd;
import l2s.gameserver.model.actor.L2Npc;
import l2s.gameserver.model.actor.instance.L2PcInstance;
import l2s.gameserver.model.quest.Quest;
import l2s.gameserver.model.quest.QuestState;
import l2s.gameserver.scripting.ScriptFile;

public class Q414_PathToOrcRaider extends Quest implements ScriptFile
{
	private static final String qn = "Q414_PathToOrcRaider";
	public final int KARUKIA = 30570;
	public final int KASMAN = 30501;
	public final int TAZEER = 31978;
	public final int GOBLIN_TOMB_RAIDER_LEADER = 20320;
	public final int KURUKA_RATMAN_LEADER = 27045;
	public final int UMBAR_ORC = 27054;
	public final int TIMORA_ORC = 27320;
	public final int GREEN_BLOOD = 1578;
	public final int GOBLIN_DWELLING_MAP = 1579;
	public final int KURUKA_RATMAN_TOOTH = 1580;
	public final int BETRAYER_UMBAR_REPORT = 1589;
	public final int HEAD_OF_BETRAYER = 1591;
	public final int TIMORA_ORCS_HEAD = 8544;
	public final int MARK_OF_RAIDER = 1592;
	
	public Q414_PathToOrcRaider(int questId, String name, String descr)
	{
		super(questId, name, descr);
		questItemIds = new int[]
		{
			KURUKA_RATMAN_TOOTH,
			GOBLIN_DWELLING_MAP,
			GREEN_BLOOD,
			HEAD_OF_BETRAYER,
			BETRAYER_UMBAR_REPORT,
			TIMORA_ORCS_HEAD
		};
		addStartNpc(KARUKIA);
		addTalkId(KASMAN);
		addTalkId(KARUKIA);
		addTalkId(TAZEER);
		addKillId(GOBLIN_TOMB_RAIDER_LEADER);
		addKillId(KURUKA_RATMAN_LEADER);
		addKillId(UMBAR_ORC);
		addKillId(TIMORA_ORC);
	}
	
	@Override
	public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
	{
		String htmltext = event;
		QuestState st = player.getQuestState(qn);
		if (event.equalsIgnoreCase("30570-05.htm"))
		{
			st.set("cond", "1");
			st.setState(Quest.STATE_STARTED);
			st.giveItems(GOBLIN_DWELLING_MAP, 1);
			st.playSound(QuestState.SOUND_ACCEPT);
		}
		else if (event.equalsIgnoreCase("to_Gludin"))
		{
			htmltext = "30570-07.htm";
			st.takeItems(KURUKA_RATMAN_TOOTH, -1);
			st.takeItems(GOBLIN_DWELLING_MAP, -1);
			st.playSound(QuestState.SOUND_MIDDLE);
			st.giveItems(BETRAYER_UMBAR_REPORT, 1);
			st.addRadar(-74490, 83275, -3374);
			st.set("cond", "3");
		}
		else if (event.equalsIgnoreCase("to_Schuttgart"))
		{
			htmltext = "30570-07a.htm";
			st.takeItems(KURUKA_RATMAN_TOOTH, -1);
			st.takeItems(GOBLIN_DWELLING_MAP, -1);
			st.addRadar(90000, -143286, -1520);
			st.playSound(QuestState.SOUND_MIDDLE);
			st.set("cond", "5");
		}
		else if (event.equalsIgnoreCase("31978-02.htm"))
		{
			st.addRadar(57502, -117576, -3700);
			st.set("cond", "6");
			st.playSound(QuestState.SOUND_MIDDLE);
		}
		return htmltext;
	}
	
	@Override
	public String onTalk(L2Npc npc, L2PcInstance player)
	{
		QuestState st = player.getQuestState(qn);
		String htmltext = "noquest";
		int npcId = npc.getNpcId();
		int cond = st.getInt("cond");
		int playerClassID = st.getPlayer().getClassId().getId();
		int playerLvl = st.getPlayer().getLevel();
		if (npcId == KARUKIA)
		{
			if (cond < 1)
			{
				if ((playerLvl >= 19) && (playerClassID == 0x2c) && (st.getQuestItemsCount(MARK_OF_RAIDER) == 0) && (st.getQuestItemsCount(GOBLIN_DWELLING_MAP) == 0))
				{
					htmltext = "30570-01.htm";
				}
				else if (playerClassID != 0x2c)
				{
					if (playerClassID == 0x2d)
					{
						htmltext = "30570-02a.htm";
					}
					else
					{
						htmltext = "30570-03.htm";
					}
				}
				else if ((playerLvl < 19) && (playerClassID == 0x2c))
				{
					htmltext = "30570-02.htm";
				}
				else if ((playerLvl >= 19) && (playerClassID == 0x2c) && (st.getQuestItemsCount(MARK_OF_RAIDER) > 0))
				{
					htmltext = "30570-04.htm";
				}
				else
				{
					htmltext = "30570-02.htm";
				}
			}
			else if ((cond == 1) && (st.getQuestItemsCount(GOBLIN_DWELLING_MAP) > 0) && (st.getQuestItemsCount(KURUKA_RATMAN_TOOTH) < 10))
			{
				htmltext = "30570-06.htm";
			}
			else if ((cond == 2) && (st.getQuestItemsCount(GOBLIN_DWELLING_MAP) > 0) && (st.getQuestItemsCount(KURUKA_RATMAN_TOOTH) > 9))
			{
				htmltext = "30570-10.htm";
			}
			else if ((cond == 3) && (st.getQuestItemsCount(BETRAYER_UMBAR_REPORT) > 0) && (st.getQuestItemsCount(HEAD_OF_BETRAYER) < 2))
			{
				htmltext = "30570-08.htm";
			}
			else if ((cond == 4) && (st.getQuestItemsCount(BETRAYER_UMBAR_REPORT) > 0) && (st.getQuestItemsCount(HEAD_OF_BETRAYER) == 2))
			{
				htmltext = "30570-09.htm";
			}
		}
		else if ((npcId == KASMAN) && (cond > 0))
		{
			if ((cond == 3) && (st.getQuestItemsCount(BETRAYER_UMBAR_REPORT) > 0) && (st.getQuestItemsCount(HEAD_OF_BETRAYER) < 1))
			{
				htmltext = "30501-01.htm";
			}
			else if ((cond == 3) && (st.getQuestItemsCount(HEAD_OF_BETRAYER) > 0) && (st.getQuestItemsCount(HEAD_OF_BETRAYER) < 2))
			{
				htmltext = "30501-02.htm";
			}
			else if ((cond == 4) && (st.getQuestItemsCount(HEAD_OF_BETRAYER) > 1))
			{
				htmltext = "30501-03.htm";
				st.exitQuest(true);
				st.giveItems(MARK_OF_RAIDER, 1);
				st.playSound(QuestState.SOUND_FINISH);
			}
		}
		else if (npcId == TAZEER)
		{
			if (cond == 5)
			{
				htmltext = "31978-01.htm";
			}
			else if ((cond == 6) && (st.getQuestItemsCount(TIMORA_ORCS_HEAD) < 1))
			{
				htmltext = "31978-03.htm";
			}
			else if ((cond == 7) && (st.getQuestItemsCount(TIMORA_ORCS_HEAD) > 0))
			{
				htmltext = "31978-04.htm";
				st.exitQuest(true);
				st.giveItems(MARK_OF_RAIDER, 1);
				st.playSound(QuestState.SOUND_FINISH);
			}
		}
		return htmltext;
	}
	
	@Override
	public String onKill(L2Npc npc, L2PcInstance player, boolean isPet)
	{
		QuestState st = checkPlayerState(player, npc, STATE_STARTED);
		if (st == null)
		{
			return null;
		}
		int npcId = npc.getNpcId();
		int cond = st.getInt("cond");
		if ((npcId == GOBLIN_TOMB_RAIDER_LEADER) && (cond == 1))
		{
			if ((st.getQuestItemsCount(GOBLIN_DWELLING_MAP) == 1) && (st.getQuestItemsCount(KURUKA_RATMAN_TOOTH) < 10) && (st.getQuestItemsCount(GREEN_BLOOD) < 40))
			{
				if ((st.getQuestItemsCount(GREEN_BLOOD) > 20) && Rnd.chance((st.getQuestItemsCount(GREEN_BLOOD) - 20) * 5))
				{
					st.takeItems(GREEN_BLOOD, -1);
					addSpawn(27045, player.getX(), player.getY(), player.getZ(), player.getHeading(), false, 0, false);
				}
				else
				{
					st.giveItems(GREEN_BLOOD, 1);
					st.playSound(QuestState.SOUND_ITEMGET);
				}
			}
		}
		else if ((npcId == KURUKA_RATMAN_LEADER) && (cond == 1))
		{
			if ((st.getQuestItemsCount(GOBLIN_DWELLING_MAP) > 0) && (st.getQuestItemsCount(KURUKA_RATMAN_TOOTH) < 10))
			{
				st.giveItems(KURUKA_RATMAN_TOOTH, 1);
				if (st.getQuestItemsCount(KURUKA_RATMAN_TOOTH) > 9)
				{
					st.set("cond", "2");
					st.playSound(QuestState.SOUND_MIDDLE);
				}
				else
				{
					st.playSound(QuestState.SOUND_ITEMGET);
				}
			}
		}
		else if ((npcId == UMBAR_ORC) && (cond == 3))
		{
			if ((st.getQuestItemsCount(BETRAYER_UMBAR_REPORT) > 0) && (st.getQuestItemsCount(HEAD_OF_BETRAYER) < 2))
			{
				st.giveItems(HEAD_OF_BETRAYER, 1);
				if (st.getQuestItemsCount(HEAD_OF_BETRAYER) > 1)
				{
					st.set("cond", "4");
					st.addRadar(-80450, 153410, -3175);
					st.playSound(QuestState.SOUND_MIDDLE);
				}
				else
				{
					st.playSound(QuestState.SOUND_ITEMGET);
				}
			}
		}
		else if ((npcId == TIMORA_ORC) && (cond == 6))
		{
			if ((st.getQuestItemsCount(TIMORA_ORCS_HEAD) < 1) && Rnd.chance(50))
			{
				st.giveItems(TIMORA_ORCS_HEAD, 1);
				st.addRadar(90000, -143286, -1520);
				st.set("cond", "7");
				st.playSound(QuestState.SOUND_MIDDLE);
			}
		}
		return null;
	}
	
	public static void onLoad()
	{
		new Q414_PathToOrcRaider(414, qn, "Path To Orc Raider");
	}
}
